import router from './router';
import nprogress from 'nprogress'; // progress bar
import 'nprogress/nprogress.css'; // progress bar style
import { getToken } from '@/utils/auth'; // get token from cookie

import store from '@/store';
// 导入动态路由
import { asyncRoutes } from '@/router';
// 定义一个白名单 目的：用户不需要登录就可以访问的页面

nprogress.configure({ showSpinner: false }); // NProgress Configuration

const whiteList = ['/login', '/404', '/error_500', '/networkError']; // no redirect whitelist

router.beforeEach(async (to, from, next) => {
	nprogress.start();

	const hasToken = getToken();

	if (hasToken) {
		if (to.path === '/login') {
			next('/');
			nprogress.done();
		} else if (to.path === '/error_500' || to.path === '/networkError') {
			next();
			nprogress.done();
		} else {
			console.log(
				'store.state.user.roleCodeList',
				store.state.user.roleCodeList.some(item => item == 'director'),
        store.state.user.roleCodeList.some(item => item == 'manager'),
				store.state.user.roleCodeList.some(item => item == 'ordinary')
			);
      if (!store.state.user.sysUser.id) {
        console.log('没有id');
				// return;
        await store.dispatch('user/getUserInfo');
				if (store.state.user.roleCodeList.some(item => item == 'ordinary')) {
					console.log('1111111');
					// return;
					// 筛选出来可以访问的路由
					const filterRoutes = asyncRoutes.filter(item => item.meta.roles);
					// const filterRoutes = [];
					store.commit('user/setRoutes', filterRoutes);
					// 动态路由添加规则
					router.addRoutes([...filterRoutes, { path: '*', redirect: '/404', hidden: true }]);

					if (router.match(to.path).path === '/404') {
						console.log('404');
						router.app.$message.warning('权限不足');
						nprogress.done();
						next('/');
					} else {
						console.log('101');
						console.log('to', to);
						// 修改vue-router的已知缺陷，需要重新执行要跳转的地址
						next({ path: to.path, query: { ...to.query }, params: { ...to.params } });
					}
					console.log('filterRoutes', filterRoutes);
				} else if (store.state.user.roleCodeList.some(item => item == 'director') || store.state.user.roleCodeList.some(item => item == 'manager')) {
					console.log('222222');
					// return;
					const filterRoutes = asyncRoutes;
					store.commit('user/setRoutes', filterRoutes);
					// 动态路由添加规则
					router.addRoutes([...filterRoutes, { path: '*', redirect: '/404', hidden: true }]);

					if (router.match(to.path).path === '/404') {
						router.app.$message.warning('权限不足');
						nprogress.done();
						next('/');
					} else {
						console.log('to', to);
						// 修改vue-router的已知缺陷，需要重新执行要跳转的地址
						next({ path: to.path, query: { ...to.query }, params: { ...to.params } });
					}
				}
			} else {
				console.log('33333');
				// 放行
				// 没权限提醒
				if (router.match(to.path).path === '/404') {
					next(from.path);
					router.app.$message.warning('权限不足');
					nprogress.done();
					next('/');
				} else {
					next();
				}
				// next()
			}
		}
	} else {
		/* has no token*/
		console.log('else00000000000000');
		if (whiteList.indexOf(to.path) !== -1) {
			// in the free login whitelist, go directly
			next();
		} else {
			// other pages that do not have permission to access are redirected to the login page.
			next(`/login?redirect=${to.path}`);
			nprogress.done();
		}
	}
});

router.afterEach(() => {
	// finish progress bar
	nprogress.done();
});
